﻿using Base.Common.HttpClient;
using HBase;
using HBase.db;
using hCommand;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic; 

namespace hconnect.http
{
    public class WCSServer : IWCSServer {
        static public  String baseUri = ConfUtil.getStr("WCSURL") ;


        public List<CoreTask> getWorkingCoreTask() {
            List<CoreTask> list = HttpRequest.getList<CoreTask>(baseUri+"/api/wcs/getWorkingCoreTask");
            return list;    
        }
        public int beginTask(BaseTask bt) {
           var value= HttpRequest.httpPost(baseUri+"/api/wcs/beginTask", JsonConvert.SerializeObject(bt));
            JObject jsonObject = JsonConvert.DeserializeObject<JObject>(value);

            if (jsonObject == null) {
                return 0;
            }
            updateCache(bt.completeParamId);
            var jsonData = jsonObject["data"];
            JObject data = (JObject)jsonData;
            return int.Parse(data.ToString());
        }

        public TransRes doStockinLocEngine(string boxCode, int stn, int eigen) {
            String url = String.Format(baseUri+"/api/wcs/doStockinLocEngine/{0}/{1}/{2}", boxCode, stn, eigen);
            return HttpRequest.getEntity<TransRes>(url);
        }

        public List<CoreWhLoc> getAllCoreWhLoc( )  { 
            String url =baseUri +"/api/wcs/getAllCoreWhLoc";
            return HttpRequest.getList<CoreWhLoc>(url);
        }

        public T getById<T>(long id) where T : class {
            Type t = typeof(T);
            String url = String.Format(baseUri +"/api/wcs/getById/{0}/{1}", t.FullName, id);
            return HttpRequest.getEntity<T>(url);
        }

        public AbsCoreStock getCoreStockByCode(string code) {
            String url = String.Format(baseUri +"/api/wcs/getCoreStockByCode/{0}", code);
            return HttpRequest.getEntity<AbsCoreStock>(url);
        }

        public List<AbsCoreStock> getCSBypkStn(int stn) {
          
            String url = String.Format(baseUri +"/api/wcs/getCSBypkStn/{0}",  stn);
            return HttpRequest.getList<AbsCoreStock>(url);
        }

        public BParam getParam(string driveType, int driveId, string key) {
            String url = String.Format(baseUri +"/api/wcs/getParam/{0}/{1}/{2}", driveType, driveId, key);
            return HttpRequest.getEntity<BParam>(url);
        }

        public List<BParam> getParams() {
          
            String url = String.Format(baseUri +"/api/wcs/getParams");
            return HttpRequest.getList<BParam>(url);
        }

        public List<BStnRelation> GetPhysicalStnRelation() {

            String url = String.Format(baseUri +"/api/wcs/GetPhysicalStnRelation");
            return HttpRequest.getList<BStnRelation>(url);
        }

        public List<BStn> GetPhysicalStns() {
            String url = String.Format(baseUri +"/api/wcs/GetPhysicalStns");
            return HttpRequest.getList<BStn>(url);
        }

        public List<CoreTaskParam> getWorkingTaskParam(int isinit) {
            String url = String.Format(baseUri +"/api/wcs/getWorkingTaskParam/"+isinit);
            return HttpRequest.getList<CoreTaskParam>(url);
        }

        public bool splitParam(long paramId) {
            String url =  baseUri +"/api/wcs/splitParam/"+ paramId;
            return HttpRequest.getJValue(url).ToString()=="true";
        }

        public bool syncWcsReult(long paramId, int realStn) {
            String url = baseUri +"/api/wcs/syncWcsReult/"+ paramId+"/"+ realStn;
            return HttpRequest.getJValue(url).ToString() == "true";
        }

        public void updateTaskParamStatus(long paramId, CoreTaskParamStatus status) {
            String url = String.Format(baseUri +"/api/wcs/updateTaskParamStatus/{0}/{1}",paramId ,(int) status);
            HttpRequest.getJValue(url);
        }

        public int updateTaskStatus(TaskStatusPersistence tsp) {
            var value = HttpRequest.httpPost(baseUri +"/api/wcs/updateTaskStatus", JsonConvert.SerializeObject(tsp));
            JObject jsonObject = JsonConvert.DeserializeObject<JObject>(value);

            if (jsonObject == null) {
                return 0;
            }

            CoreTaskParam p = MyCache.queryUnique<CoreTaskParam>((a) => {
                return a.taskNo==tsp.taskNo;
            },"CompleteParam 过多"+tsp.taskNo);

            updateCache(p.id);
            var jsonData = jsonObject["data"];
            JObject data = (JObject)jsonData;
            return int.Parse(data.ToString());
        }

        public bool updateWcsAutoActive(int craneId, int status) {
            String url = baseUri +"/api/wcs/updateWcsAutoActive/"+ craneId + "/" + status;
            return HttpRequest.getJValue(url).ToString() == "true";
        }

        public String updateTaskPriority(long id,int p) {
            String url = baseUri+"/api/wcs/updateTaskPriority/"+id+"/"+p;
            String res = HttpRequest.getJValue(url).ToString();
            updateCache(id);
            return res;
        }

        public String updateParamAt(long paramId,int stn) {
            String url = baseUri+"/api/wcs/updateParamAt/"+paramId+"/"+stn;
            String res= HttpRequest.getJValue(url).ToString();
            updateCache(paramId);
            return res;
        }

        public String doWcsMoveTask(int from,int to) {
            String url = baseUri+"/api/wcs/doWcsMoveTask/"+from+"/"+to;
            return HttpRequest.getJValue(url).ToString();
        }

        public String paramDelete(long id) {
            String url = baseUri+"/api/wcs/paramDelete/"+id ;
            var res= HttpRequest.getJValue(url).ToString(); ;
            return res;
        }

        void updateCache(long  id) {

            CoreTaskParam p = getById<CoreTaskParam>(id);
            MyCache.addOver(p);
            splitParam(p.id);
        }

        public String change99999(long paramid) {
            String url = baseUri + "/api/wcs/change99999/" + paramid;
            var res = HttpRequest.getJValue(url).ToString();
            ;
            return res;
        }
    }
}
